#ifndef SESSIONMANAGER_H
#define SESSIONMANAGER_H

#include <unordered_map>

#include "Poco/RWLock.h"
#include "Poco/HashMap.h"
#include "Poco/AutoPtr.h"
#include "Poco/Net/SocketNotification.h"

#include "./Common.h"

namespace Reactor
{
class Session;
class HeartBeatWorker;

constexpr int SEGMENTS = 16;
struct HashSegment
{
	Poco::RWLock _lock;
	std::unordered_map<std::string, std::shared_ptr<Session>> _sessionTable;
};
using HashSegmentPtr = std::shared_ptr<HashSegment>;
using HashSegmentIndex = Poco::UInt32;
using HashSegmentVector = Poco::HashMap<HashSegmentIndex, HashSegmentPtr>;

struct SessionManagerPrivate
{
	HashSegmentVector _sessionTables;
};

class MOUDLE_API SessionManager : public TSingleton<SessionManager>
{
	friend class TSingleton<SessionManager>;
	friend class HeartBeatWorker;
public:
	~SessionManager();

	void addSession(const std::shared_ptr<Session>& _session);
	std::shared_ptr<Session> session(const std::string& _id);
	void removeSession(const std::string& _sessionId);

protected:
	HashSegmentVector& RefHashSegmentVector();
	SessionManager();
	void initialize();
	void destroy();

private:
	std::shared_ptr<SessionManagerPrivate> d;
};

}
#endif // !SESSIONMANAGER_H
